﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
namespace ReflectionStudio.Controls.Controls
{

    public abstract class NotificationObject : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;



        protected virtual void RaisePropertyChanged(string propertyName)
        {

            PropertyChangedEventHandler handler = this.PropertyChanged;

            if (handler != null)
            {

                handler(this, new PropertyChangedEventArgs(propertyName));

            }

        } 

        protected void RaisePropertyChanged(params string[] propertyNames)
        {

            if (propertyNames == null) throw new ArgumentNullException("propertyNames");



            foreach (var name in propertyNames)
            {

                this.RaisePropertyChanged(name);

            }

        }



        protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {

            var propertyName = ExtractPropertyName(propertyExpression);

            this.RaisePropertyChanged(propertyName);

        }



        public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
        {

            if (propertyExpression == null)
            {

                throw new ArgumentNullException("propertyExpression");

            } 

            var memberExpression = propertyExpression.Body as MemberExpression;

            if (memberExpression == null)
            {

                throw new ArgumentException("PropertySupport_NotMemberAccessExpression_Exception", "propertyExpression");

            }   
 
            return memberExpression.Member.Name;

        }

    }
}
